<?php

namespace api\controllers;

use Yii;
use common\models\CategoryCompany;
use common\models\Category;

class CategoryController extends ApiController{
    
    /**
     * 
     */
    public function actionTree(){
        $data = CategoryCompany::find()->dataFancytree($this->userData->id_category);
        $children = [];
        if(count($data[0]['children']) > 0){
            $children = $data[0]['children'];
        }
        $first = [
            [
                'key' => 0,
                'title' => 'Tất cả',
                'children' => []
            ],
            /*[
                'key' => $this->userData->id_category,
                'title' => 'Chưa xác định nhóm',
                'children' => []
            ]*/
            
        ];
        $objects = array_merge($first,$children);
        //
        $result = ['error' => false,'message' => "",'objects' => $objects];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /**
     * 
     */
    public function actionIndex(){
        $category_id = Yii::$app->request->get('category_id',0);
        $root = CategoryCompany::findOne($this->userData->id_category);
        $objects[] = [
            'id' => $root->id_category_company,
            'name' => 'Nhóm gốc'
        ];
        $array = CategoryCompany::getListCategory($this->userData->id_category,$category_id);
        $objects = array_merge($objects,$array);
        //
        $result = ['error' => false,'message' => "",'objects' => $objects];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
        
    }
    
    /**
     * 
     */
    public function actionCategoryWithoutRoot(){
        $objects = CategoryCompany::getListCategory($this->userData->id_category);
        //
        $result = ['error' => false,'message' => "",'objects' => $objects];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /**
     * 
     */
    public function actionCategoriesOnline(){
        $objects = Category::getListLastCatagoryObject();
        //
        $result = ['error' => false,'message' => "",'objects' => $objects];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /**
     * 
     */
    public function actionEdit(){
        $error = true;
        $message = "";
        $objects = [];
        //
        $category_id = Yii::$app->request->post('category_id',0);
        $name = Yii::$app->request->post('name',"");
        $parent = Yii::$app->request->post('parent',0);
        $obParent = CategoryCompany::findOne($parent);
        if($obParent){
            if($category_id == 0){
                $category = new CategoryCompany();
            }else{
                $category = CategoryCompany::findOne($category_id);
            }
            if($category){
                $category->name = $name;
                $category->id_company = $this->userData->id_company;
                $category->parents = $parent;
                if($category_id == 0){
                    $category->appendTo($obParent);
                }else{
                    $category->saveNode();
                    $category->moveAsFirst($obParent);
                }
                
                $error = FALSE;
            }
        }
        
        //
        $result = ['error' => $error,'message' => $message,'objects' => $objects];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
}